using System;
using System.Collections.Generic;
using System.Text;
using Csla.Data;
using Csla;
using Active.Core.DB;

namespace Active.Core
{
    [Serializable()]
    public class CustomerOrder : BusinessBase<CustomerOrder>
    {
        #region Business Methods

        // TODO: add your own fields, properties and methods
        private int _id;

        public int Id
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _id;
            }
        }

        string _description = string.Empty;
        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _description;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_description != value)
                {
                    _description = value;
                    PropertyHasChanged();
                }
            }
        }

        double _discount;
        /// <summary>
        /// Gets or sets the discount.
        /// </summary>
        /// <value>The discount.</value>
        public double Discount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _discount;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_discount.Equals(value))
                {
                    _discount = value;
                    PropertyHasChanged();
                }
            }
        }

        string _duration = string.Empty;
        /// <summary>
        /// Gets or sets the duration.
        /// </summary>
        /// <value>The duration.</value>
        public string Duration
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _duration;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_duration!=value)
                {
                    _duration = value;
                    PropertyHasChanged();
                }
            }
        }

        string _customerEmployee = string.Empty;
        /// <summary>
        /// Gets or sets the customer employee.
        /// </summary>
        /// <value>The customer employee.</value>
        public string CustomerEmployee
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _customerEmployee;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_customerEmployee.Equals(value))
                {
                    _customerEmployee = value;
                    PropertyHasChanged();
                }
            }
        }

        int _orderId;
        /// <summary>
        /// Gets or sets the order id.
        /// </summary>
        /// <value>The order id.</value>
        public int OrderId
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _orderId;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_orderId.Equals(value))
                {
                    _orderId = value;
                    PropertyHasChanged();
                }
            }
        }

        string _position = string.Empty;
        /// <summary>
        /// Gets or sets the position.
        /// </summary>
        /// <value>The position.</value>
        public string Position
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _position;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_position!=value)
                {
                    _position = value;
                    PropertyHasChanged();
                }
            }
        }

        decimal _payout;
        /// <summary>
        /// Gets or sets the payout.
        /// </summary>
        /// <value>The payout.</value>
        public decimal Payout
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _payout;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_payout.Equals(value))
                {
                    _payout = value;
                    PropertyHasChanged();
                }
            }
        }

        int _times;
        /// <summary>
        /// Gets or sets the times.
        /// </summary>
        /// <value>The times.</value>
        public int Times
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _times;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_times.Equals(value))
                {
                    _times = value;
                    PropertyHasChanged();
                }
            }
        }

        string _timeSlot = string.Empty;
        /// <summary>
        /// Gets or sets the time slot.
        /// </summary>
        /// <value>The time slot.</value>
        public string TimeSlot
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _timeSlot;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_timeSlot!=value)
                {
                    _timeSlot = value;
                    PropertyHasChanged();
                }
            }
        }

        decimal _unitPrice;
        /// <summary>
        /// Gets or sets the unit price.
        /// </summary>
        /// <value>The unit price.</value>
        public decimal UnitPrice
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _unitPrice;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_unitPrice.Equals(value))
                {
                    _unitPrice = value;
                    PropertyHasChanged();
                }
            }
        }

        string _channel = string.Empty;
        /// <summary>
        /// Gets or sets the channel.
        /// </summary>
        /// <value>The channel.</value>
        public string Channel
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _channel;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_channel!=value)
                {
                    _channel = value;
                    PropertyHasChanged();
                }
            }
        }

        string _campaignPeriod = string.Empty;
        /// <summary>
        /// Gets or sets the campaign period.
        /// </summary>
        /// <value>The campaign period.</value>
        public string CampaignPeriod
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _campaignPeriod;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_campaignPeriod!=value)
                {
                    _campaignPeriod = value;
                    PropertyHasChanged();
                }
            }
        }

        OrderType _category = OrderType.Pay;
        /// <summary>
        /// Gets or sets the category.
        /// </summary>
        /// <value>The category.</value>
        public OrderType Category
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _category;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_category.Equals(value))
                {
                    _category = value;
                    PropertyHasChanged();
                }
            }
        }

        decimal _realPayout;
        public decimal RealPayout
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _realPayout;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_realPayout.Equals(value))
                {
                    _realPayout = value;
                    PropertyHasChanged();
                }
            }
        }

        double _realDiscount;
        public double RealDiscount
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _realDiscount;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_realDiscount.Equals(value))
                {
                    _realDiscount = value;
                    PropertyHasChanged();
                }
            }
        }

        private MediaOrders _mediaOrders = null;
        public MediaOrders MediaOrders
        {
            get
            {
                if (_mediaOrders == null)
                {
                    _mediaOrders = MediaOrders.GetMediaOrders(_id);
                }
                return _mediaOrders;
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        internal static CustomerOrder NewCustomerOrder()
        {
            return new CustomerOrder();
        }

        internal static CustomerOrder GetCustomerOrder(SafeDataReader dr)
        {
            return new CustomerOrder(dr);
        }

        private CustomerOrder()
        {
            MarkAsChild();
        }

        private CustomerOrder(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            _id = dr.GetInt32("Id");
            _campaignPeriod = dr.GetString("CampaignPeriod");
            _category = (OrderType)dr.GetInt32("Category");
            _channel = dr.GetString("Channel");
            _customerEmployee = dr.GetString("CustomerEmployee");
            _description = dr.GetString("Description");
            _discount = dr.GetDouble("Discount");
            _duration = dr.GetString("Duration");
            _orderId = dr.GetInt32("OrderId");
            _payout = dr.GetDecimal("Payout");
            _position = dr.GetString("Position");
            _realDiscount = dr.GetDouble("RealDiscount");
            _realPayout = dr.GetDecimal("RealPayout");
            _times = dr.GetInt32("Times");
            _timeSlot = dr.GetString("TimeSlot");
            _unitPrice = dr.GetDecimal("UnitPrice");
            
            MarkOld();
        }

        internal void Insert(Order parent)
        {
            _orderId = parent.Id;
            _customerEmployee = parent.CustomerEmployee;
            NBearLite.InsertSqlSection insert =
                DataService.DBService.Insert(Tables.CustomerOrders)
                .AddColumn(Tables.CustomerOrders.CampaignPeriod, _campaignPeriod)
                .AddColumn(Tables.CustomerOrders.Category, _category)
                .AddColumn(Tables.CustomerOrders.Channel, _channel)
                .AddColumn(Tables.CustomerOrders.CustomerEmployee, _customerEmployee)
                .AddColumn(Tables.CustomerOrders.Description, _description)
                .AddColumn(Tables.CustomerOrders.Discount, _discount)
                .AddColumn(Tables.CustomerOrders.Duration, _duration)
                .AddColumn(Tables.CustomerOrders.OrderId, _orderId)
                .AddColumn(Tables.CustomerOrders.Payout, _payout)
                .AddColumn(Tables.CustomerOrders.Position, _position)
                .AddColumn(Tables.CustomerOrders.RealDiscount, _realDiscount)
                .AddColumn(Tables.CustomerOrders.RealPayout, _realPayout)
                .AddColumn(Tables.CustomerOrders.Times, _times)
                .AddColumn(Tables.CustomerOrders.TimeSlot, _timeSlot)
                .AddColumn(Tables.CustomerOrders.UnitPrice, _unitPrice);
            _id = insert.ExecuteReturnAutoIncrementID(Tables.CustomerOrders.Id);
            MarkOld();
        }

        internal void Update(Order parent)
        {
            _orderId = parent.Id;
            NBearLite.UpdateSqlSection update =
                DataService.DBService.Update(Tables.CustomerOrders)
                .AddColumn(Tables.CustomerOrders.CampaignPeriod, _campaignPeriod)
                .AddColumn(Tables.CustomerOrders.Category, _category)
                .AddColumn(Tables.CustomerOrders.Channel, _channel)
                //.AddColumn(Tables.CustomerOrders.CustomerEmployee, _customerEmployee)
                .AddColumn(Tables.CustomerOrders.Description, _description)
                .AddColumn(Tables.CustomerOrders.Discount, _discount)
                .AddColumn(Tables.CustomerOrders.Duration, _duration)
                .AddColumn(Tables.CustomerOrders.OrderId, _orderId)
                .AddColumn(Tables.CustomerOrders.Payout, _payout)
                .AddColumn(Tables.CustomerOrders.Position, _position)
                .AddColumn(Tables.CustomerOrders.RealDiscount, _realDiscount)
                .AddColumn(Tables.CustomerOrders.RealPayout, _realPayout)
                .AddColumn(Tables.CustomerOrders.Times, _times)
                .AddColumn(Tables.CustomerOrders.TimeSlot, _timeSlot)
                .AddColumn(Tables.CustomerOrders.UnitPrice, _unitPrice)
                .Where(Tables.CustomerOrders.Id == _id);
            update.Execute();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            DataService.DBService.Delete(Tables.CustomerOrders)
                .Where(Tables.CustomerOrders.Id == _id).Execute();
            MarkNew();
        }

        #endregion
    }
}
